<?php

namespace App\Console\Commands\WorkOrder;

use App\Enums\WorkOrder\OrderEnum;
use App\Models\WorkOrder\WorkOrderUserRoleModel;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Console\Command;

class InitUserRole extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workOrder:init-user-role';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化工单处理人数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        /**
         * 仓储配送问题工单对应供应链部门佐为/克鲁德，
         * 品质异常问题工单对应给品控处理人（波赛西、尤莉、葫芦娃、辛德瑞拉）。
         * 功能开通工单对应给产品处理人（扶苏、颜路）客服角色 可以看到所有类型的工单
         */
        $arr = [
            [
                'userId'         => 1631, // 露西
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_1,
                'problemPid'     => 0,
                'problemChildId' => 0,
            ],
            [
                'userId'         => 1943, // 克鲁德
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_3,
                'problemPid'     => OrderEnum::PROBLEM_PID_1,
                'problemChildId' => 0,
            ],
            [
                'userId'         => 1944, // 波赛西
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_2,
                'problemPid'     => OrderEnum::PROBLEM_PID_2,
                'problemChildId' => 0,
            ],
            [
                'userId'         => 1945, // 尤莉
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_2,
                'problemPid'     => OrderEnum::PROBLEM_PID_2,
                'problemChildId' => 0,
            ],
            [
                'userId'         => 1946, // 葫芦娃
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_2,
                'problemPid'     => OrderEnum::PROBLEM_PID_2,
                'problemChildId' => 0,
            ],
            [
                'userId'         => 1856, // 辛德瑞拉
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_2,
                'problemPid'     => OrderEnum::PROBLEM_PID_2,
                'problemChildId' => 0,
            ],
            [
                'userId'         => 199, // 颜路
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_4,
                'problemPid'     => OrderEnum::PROBLEM_PID_5,
                'problemChildId' => OrderEnum::PROBLEM_PID_5_CHILDREN_100,
            ],
            [
                'userId'         => 1983, // 灰原哀
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_4,
                'problemPid'     => OrderEnum::PROBLEM_PID_5,
                'problemChildId' => OrderEnum::PROBLEM_PID_5_CHILDREN_101,
            ],
            [
                'userId'         => 1983, // 灰原哀
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_4,
                'problemPid'     => OrderEnum::PROBLEM_PID_5,
                'problemChildId' => OrderEnum::PROBLEM_PID_5_CHILDREN_102,
            ],
            [
                'userId'         => 96, // 岸边露伴
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_4,
                'problemPid'     => OrderEnum::PROBLEM_PID_5,
                'problemChildId' => OrderEnum::PROBLEM_PID_5_CHILDREN_103,
            ],
            [
                'userId'         => 120, // 欧肯
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_4,
                'problemPid'     => OrderEnum::PROBLEM_PID_5,
                'problemChildId' => OrderEnum::PROBLEM_PID_5_CHILDREN_104,
            ],
            [
                'userId'         => 1996, // 小岛元太
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_5,
            ],
            [
                'userId'         => 1996, // 小岛元太
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_6,
            ],
            [
                'userId'         => 1996, // 小岛元太
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_7,
            ],
            [
                'userId'         => 2110, // 木暮公延
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_1,
            ],
            [
                'userId'         => 2110, // 木暮公延
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_2,
            ],
            [
                'userId'         => 2110, // 木暮公延
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_13,
            ],
            [
                'userId'         => 2110, // 木暮公延
                'role'           => WorkOrderUserRoleModel::ROLE_TYPE_5,
                'problemPid'     => OrderEnum::PROBLEM_PID_4,
                'problemChildId' => OrderEnum::PROBLEM_PID_4_CHILDREN_8,
            ],
        ];

        if (config('app.env') == 'prod') {
            WorkOrderUserRoleModel::query()->truncate();
        } elseif (config('app.env') == 'pre') {
            WorkOrderUserRoleModel::query()->truncate();

            foreach ($arr as &$v) {
                if ($v['role'] == WorkOrderUserRoleModel::ROLE_TYPE_4) {
                    if ($v['problemChildId'] >= 102) {
                        $v['userId'] = 199;
                    } else {
                        $v['userId'] = 89;
                    }
                }
            }
        } else {
            foreach ($arr as &$v) {
                $v['userId'] = 6;
            }
        }

        $uids      = collect($arr)->pluck('userId')->unique()->values()->toArray();
        $userInfos = collect(RmsUserService::getRmsUserInfoByUserIds($uids)['data'] ?? [])->keyBy('id');

        collect($arr)
            ->map(function ($item) use ($userInfos) {

                $user = $userInfos[$item['userId']] ?? [];

                return [
                    'name'           => $user['username'],
                    'feishuId'       => $user['feishuUserId'],
                    'mobile'         => $user['mobile'],
                    'role'           => $item['role'],
                    'userId'         => $user['id'],
                    'problemPid'     => $item['problemPid'],
                    'problemChildId' => $item['problemChildId'],
                ];
            })
            ->each(function ($item) {

                $res = WorkOrderUserRoleModel::query()
                                             ->updateOrCreate([
                                                 'userId'         => $item['userId'],
                                                 'role'           => $item['role'],
                                                 'problemPid'     => $item['problemPid'],
                                                 'problemChildId' => $item['problemChildId']
                                             ], $item);

                dump($res);
            });
    }
}
